<template>
  <div class="member-card">
    <div class="card-header">
      <span class="card-title">会员卡</span>
      <span @click="onRecordlink" class="record-link"
        >充值记录 <i class="el-icon-arrow-right"></i
      ></span>
    </div>
    <div class="line"></div>
    <div class="balance-section">
      <span class="balance-label">余额</span>
      <div class="balance-amount">¥{{ (balance/100).toFixed(2)}}</div>
      <button class="recharge-button" @click="onRecharge">充值</button>
    </div>
    <div class="recent-recharge">最近充值: {{ lastRechargeTime }}</div>
    <!-- 充值记录 -->
    <rechargeRecord ref="rechargeRecordRef" @refresh="handleChargeConfirm" />
    <!-- 充值 -->
    <chargeDialog
      ref="chargeDialogRef"
      :visible="showChargeDialog"
      @close="showChargeDialog = false"
      @confirm="handleChargeConfirm"
      :patientId="props.patientId"
    />
  </div>
</template>

<script lang="ts" setup>
import rechargeRecord from './rechargeRecord.vue';
import chargeDialog from './ChargeDialog.vue';
const props = defineProps<{
  patientId: any;
  patientDetail: any;
}>();
const emit = defineEmits<{
  (e: 'recharge'): void;
}>();


const balance = ref(props.patientDetail?.balance || 0.0);
const lastRechargeTime = ref(props.patientDetail?.lastRechargeTime || '');
const rechargeRecordRef = ref<InstanceType<typeof rechargeRecord> | null>(null);
  watch(
  () => props.patientDetail,
  (newColor) => {
    //console.log(newColor);
    
    balance.value = newColor?.balance;
    lastRechargeTime.value = newColor?.lastRechargeTime;
  },{ immediate: true }
);
  const showChargeDialog = ref(false);
const onRecordlink = () => {
  //console.log('点击了充值记录');
  rechargeRecordRef.value?.open(props.patientId);
};
const onRecharge = () => {
  //console.log('点击了充值');
  showChargeDialog.value = true;
};
const handleChargeConfirm = (amount: number) => {
  //console.log('充值金额:', amount);
  showChargeDialog.value = false;
  emit('recharge');
  // 这里可以添加充值成功后的逻辑，比如刷新余额等
};
</script>

<style scoped>
.member-card {
  border-radius: 10px;
  padding: 20px;
  width: 300px;
  margin: 0 auto;
  height: 420px;
  background: #ffffff;
  border-radius: 12px 12px 12px 12px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.card-title {
  font-size: 18px;
  font-weight: bold;
}

.record-link {
  color: #666;
  cursor: pointer;
}

.balance-section {
  text-align: center;
  margin-bottom: 20px;
  margin-top: 42px;
}

.balance-label {
  font-size: 14px;
  color: #666;
  display: block;
  margin-bottom: 5px;
}

.balance-amount {
  font-size: 36px;
  font-weight: bold;
  color: #333;
}

.recharge-button {
  background-color: transparent;
  border: 1px solid #00b3b3;
  color: #00b3b3;
  padding: 8px 20px;
  border-radius: 5px;
  cursor: pointer;
  margin-top: 38px;
  transition: background-color 0.3s ease, color 0.3s ease;
}

.recharge-button:hover {
  background-color: #00b3b3;
  color: white;
}

.recent-recharge {
  color: #666;
  font-size: 12px;
  text-align: center;
}
.line {
  width: 100%;
  height: 0px;
  border: 1px solid #e7e7e7;
}
</style>
